package com.dr.controller.index;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dr.entity.Comment;
import com.dr.entity.Product;
import com.dr.entity.Producttype;
import com.dr.service.CommentWebService;
import com.dr.service.ProductService;
import com.dr.service.ProductWebService;
import com.dr.service.ProducttypeService;

@Controller
@RequestMapping("/index/")
public class ProductWebController {
	// 注入service
	@Autowired
	 private ProductService productService;
	@Autowired
	private ProducttypeService producttypeService;
	@Autowired 
	private ProductWebService productWebService;
	@Autowired
	private CommentWebService commentWebService;
	
	/**
	 * 全查
	 * 
	 * @param p
	 * @return 以Map的方式返回查询到的数据和行数
	 */
	@RequestMapping("/AllIndexProduct.do")
	public String AllIndexProduct(String name,Integer tid,Model model) {
		List<Product> listTj=productService.findWebIndexProduct(name, tid, 1);
		List<Product> listQt=productService.findWebIndexProduct(name, tid, 0);
		List<Producttype> listFl=producttypeService.findOneLevel();
		for (Producttype p : listFl) {
			p.setSonlist(producttypeService.findSonByid(p.getId()));
		}
		//保存数据
		model.addAttribute("listTj",listTj);
		model.addAttribute("listQt",listQt);
		model.addAttribute("listFl",listFl);
		return "idea.jsp";
	}

	/**
	 * 产品详情
	 */
	@RequestMapping("/findAll.do")
	public String findAll(Integer id,Model model) {
		Product product=productWebService.findAll(id);
		List<Producttype> listFl=producttypeService.findOneLevel();
		List<Comment> comments = commentWebService.AllComment(id);
		for (Producttype p : listFl) {
			p.setSonlist(producttypeService.findSonByid(p.getId()));
		}
		List<Product> tjList=productService.findWebIndexProduct(null, null, 1);
		//获取尺寸
		String[] sizeArr=product.getSize().split("，");
		model.addAttribute("comments", comments);
		model.addAttribute("product",product);
		model.addAttribute("listFl",listFl);
		model.addAttribute("tjList",tjList);
		model.addAttribute("sizeArr",sizeArr);
		return "proDetail.jsp";
		
	}
	
}
